import dayjs from "dayjs";
import {storeBindingsBehavior} from 'mobx-miniprogram-bindings'
import base from '@src/store/base';
import {getThemeConfig} from "@src/api/base";
import mall from "@src/store/mall";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    actions: {
      initReady: 'initReady'
    }
  }, {
    store: mall,
    fields: ['curLocation', 'storeId'],
    actions: ['initLocation', 'setAddress', 'setStoreId']
  }
  ],
  options: {
    addGlobalClass: true
  },
  properties: {
    isMember: {
      type: Boolean,
      value: false
    },
    splashScreen: {
      type: Object,
      value: {type: 1}
    },
    businessLogo: {
      type: String,
      value: ""
    },
    businessName: {
      type: String,
      value: ""
    },
    // countDownTime: {
    //   type: String,
    //   value: ""
    // }
  },
  data: {
    realSrc: '',
    showDate: '',
    countDownTime: -1,
    timerId: null,
    setInter: null,
    hasInitSplashScreen: false,
  },
  observers: {
    'splashScreen': function (splashScreen) {
      if (!splashScreen || this.data.hasInitSplashScreen) {
        return;
      }
      this.init()
    }
  },
  lifetimes: {
    attached: function () {
      setTimeout(()=> {
        if (this.data.hasInitSplashScreen) {
          return;
        }
        this.ready();
      }, 20000)
    }
  },
  pageLifetimes: {
    show: function () {
      console.log("执行show2")
    },
  },
  methods: {
    init() {
      let str = "星期" + "日一二三四五六".charAt(new Date().getDay());
      let showDate = dayjs(new Date()).format("YYYY年MM月DD日，") + str;
      this.setData({showDate: showDate});
      let self = this;
      let splashScreen = this.data.splashScreen;
      this.setData({
        hasInitSplashScreen: true,
        countDownTime: splashScreen.pause
      })
      let timerId = setInterval(() => {
        let countDownTime = this.data.countDownTime;
        if (countDownTime <= 0) {
          this.ready();
        } else {
          self.setData({
            countDownTime: countDownTime - 1
          })
        }
      }, 1000);
      this.setData({timerId: timerId})
    },
    skipTime() {
      console.log("跳过");
      console.log("this.data.splashScreen.skip", this.data.splashScreen.skip)
      if (this.data.splashScreen.skip) {
        this.ready();
      }
    },
    ready() {
      if (this.data.timerId) {
        clearInterval(this.data.timerId);
        this.setData({timerId: null})
      }
      this.initReady()
      setTimeout(() => {
        this.initLocation(false, false);
      }, 600)
    }
  }
})
